import java.util.HashMap;

/**
* @description: leetCode 周赛303 t2
* @author hewei
* @date 2022/7/24 10:34
* @version 1.0
*/

public class EqualPairs {

    public int equalPairs(int[][] grid) {
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0; i < grid.length; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < grid[0].length; j++) {
                stringBuilder.append(grid[i][j]);
                stringBuilder.append("#");
            }
            map.put(stringBuilder.toString(), map.getOrDefault(stringBuilder.toString(), 0) + 1);
        }
        int ans = 0;
        for (int i = 0; i < grid[0].length; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < grid.length; j++) {
                stringBuilder.append(grid[j][i]);
                stringBuilder.append("#");
            }
            ans += map.getOrDefault(stringBuilder.toString(), 0);
        }
        return ans;
    }
}
